package Service;

import domain.User;
import utils.Tools;

import java.io.IOException;
import java.util.Scanner;

public class UserService {
    static Tools tools = new Tools();

    public User login(String userId, String password) throws IOException {
        User user = tools.getUser(userId);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    public static void changePassword(String userid, String password) throws IOException {
        User user = tools.getUser(userid);
        user.setPassword(password);
        tools.setUser(user);
    }

    public static void updateUserInfo(User user) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入新的姓名（当前：" + user.getName() + "）：");
        String name = scanner.nextLine();
        System.out.print("请输入新的电话（当前：" + user.getPhone() + "）：");
        String phone = scanner.nextLine();
        System.out.print("请输入新的邮箱（当前：" + user.getEmail() + "）：");
        String email = scanner.nextLine();

        if (!name.isEmpty()) user.setName(name);
        if (!phone.isEmpty()) user.setPhone(phone);
        if (!email.isEmpty()) user.setEmail(email);

        System.out.println("信息更新成功！");
        tools.setUser(user);
    }

    public static User userRole(User user) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int id = tools.getID("userId");
        user.setUserId("S" + id);
        id++;
        tools.setID("userId", id);
        System.out.print("请输入用户姓名：");
        user.setName(scanner.nextLine());
        System.out.print("请输入用户密码：");
        user.setPassword(scanner.nextLine());
        System.out.print("请输入用户电话：");
        user.setPhone(scanner.nextLine());
        System.out.print("请输入用户邮箱：");
        user.setEmail(scanner.nextLine());
        return user;
    }

    public static void registerUser() throws IOException {
        User user = new User();
        User role  = userRole(user);
        role.setRole("user");
        tools.setUser(role);
        System.out.println("注册成功！");
        System.out.println("您的登录id为：" + role.getUserId() + ",请牢记");
    }
    public static void addUser() throws IOException {
        User user = new User();
        User role  = userRole(user);
        System.out.print("请输入用户角色(user/clerk)：");
        Scanner scanner = new Scanner(System.in);
        role.setRole(scanner.nextLine());
        tools.setUser(role);
        System.out.println("添加成功！");
        System.out.println("该账号的登录id为：" + role.getUserId() + ",请牢记");
    }

    public static void deleteUser(String userId) throws IOException {
        tools.removeUserById(userId);

        System.out.println("用户删除成功！");
        CardService.deleteCard(userId);
    }

    public static void queryUserInfo(String userId) throws IOException {
        User user = tools.getUser(userId);
        if (user != null) {
        System.out.println("用户信息：");
        System.out.println("姓名：" + user.getName());
        System.out.println("电话：" + user.getPhone());
        System.out.println("邮箱：" + user.getEmail());
        }
    }
}